
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>从JavaScript迁移到TypeScript · TypeScript Handbook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="Patrick Zhong">
        
        
    
    <link rel="stylesheet" href="../../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../" />
    
    
    <link rel="prev" href="Angular 2.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../../">
            
                <a href="../../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="./">
            
                <a href="./">
            
                    
                    快速上手
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="TypeScript in 5 minutes.html">
            
                <a href="TypeScript in 5 minutes.html">
            
                    
                    5分钟了解TypeScript
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="ASP.NET Core.html">
            
                <a href="ASP.NET Core.html">
            
                    
                    ASP.NET Core
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="ASP.NET 4.html">
            
                <a href="ASP.NET 4.html">
            
                    
                    ASP.NET 4
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="Gulp.html">
            
                <a href="Gulp.html">
            
                    
                    Gulp
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="Knockout.html">
            
                <a href="Knockout.html">
            
                    
                    Knockout.js
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="React & Webpack.html">
            
                <a href="React & Webpack.html">
            
                    
                    React与webpack
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="React.html">
            
                <a href="React.html">
            
                    
                    React
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8" data-path="Angular 2.html">
            
                <a href="Angular 2.html">
            
                    
                    Angular 2
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.2.9" data-path="Migrating from JavaScript.html">
            
                <a href="Migrating from JavaScript.html">
            
                    
                    从JavaScript迁移到TypeScript
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../">
            
                <a href="../">
            
                    
                    手册
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../Basic Types.html">
            
                <a href="../Basic Types.html">
            
                    
                    基础类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../Variable Declarations.html">
            
                <a href="../Variable Declarations.html">
            
                    
                    变量声明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../Interfaces.html">
            
                <a href="../Interfaces.html">
            
                    
                    接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../Classes.html">
            
                <a href="../Classes.html">
            
                    
                    类
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../Functions.html">
            
                <a href="../Functions.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../Generics.html">
            
                <a href="../Generics.html">
            
                    
                    泛型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="../Enums.html">
            
                <a href="../Enums.html">
            
                    
                    枚举
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="../Type Inference.html">
            
                <a href="../Type Inference.html">
            
                    
                    类型推论
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="../Type Compatibility.html">
            
                <a href="../Type Compatibility.html">
            
                    
                    类型兼容性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.10" data-path="../Advanced Types.html">
            
                <a href="../Advanced Types.html">
            
                    
                    高级类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.11" data-path="../Utilit Types.md">
            
                <span>
            
                    
                    实用工具类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.12" data-path="../Symbols.html">
            
                <a href="../Symbols.html">
            
                    
                    Symbols
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.13" data-path="../Iterators and Generators.html">
            
                <a href="../Iterators and Generators.html">
            
                    
                    Iterators 和 Generators
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.14" data-path="../Modules.html">
            
                <a href="../Modules.html">
            
                    
                    模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.15" data-path="../Namespaces.html">
            
                <a href="../Namespaces.html">
            
                    
                    命名空间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.16" data-path="../Namespaces and Modules.html">
            
                <a href="../Namespaces and Modules.html">
            
                    
                    命名空间和模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.17" data-path="../Module Resolution.html">
            
                <a href="../Module Resolution.html">
            
                    
                    模块解析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.18" data-path="../Declaration Merging.html">
            
                <a href="../Declaration Merging.html">
            
                    
                    声明合并
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.19" data-path="../Writing Definition Files.html">
            
                <a href="../Writing Definition Files.html">
            
                    
                    书写.d.ts文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.20" data-path="../JSX.html">
            
                <a href="../JSX.html">
            
                    
                    JSX
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.21" data-path="../Decorators.html">
            
                <a href="../Decorators.html">
            
                    
                    Decorators
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.22" data-path="../Mixins.html">
            
                <a href="../Mixins.html">
            
                    
                    混入
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.23" data-path="../Triple-Slash Directives.html">
            
                <a href="../Triple-Slash Directives.html">
            
                    
                    三斜线指令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.24" data-path="../Type Checking JavaScript Files.html">
            
                <a href="../Type Checking JavaScript Files.html">
            
                    
                    JavaScript文件里的类型检查
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../declaration files/Introduction.html">
            
                <a href="../declaration files/Introduction.html">
            
                    
                    如何书写声明文件
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../declaration files/Library Structures.html">
            
                <a href="../declaration files/Library Structures.html">
            
                    
                    结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../declaration files/Do's and Don'ts.html">
            
                <a href="../declaration files/Do's and Don'ts.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../declaration files/By Example.html">
            
                <a href="../declaration files/By Example.html">
            
                    
                    举例
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../declaration files/Deep Dive.html">
            
                <a href="../declaration files/Deep Dive.html">
            
                    
                    深入
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../declaration files/Publishing.html">
            
                <a href="../declaration files/Publishing.html">
            
                    
                    发布
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../declaration files/Consumption.html">
            
                <a href="../declaration files/Consumption.html">
            
                    
                    使用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../tsconfig.json.html">
            
                <a href="../tsconfig.json.html">
            
                    
                    工程配置
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../tsconfig.json.html">
            
                <a href="../tsconfig.json.html">
            
                    
                    tsconfig.json
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../Project References.html">
            
                <a href="../Project References.html">
            
                    
                    工程引用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../Typings for NPM Packages.html">
            
                <a href="../Typings for NPM Packages.html">
            
                    
                    NPM包的类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../Compiler Options.html">
            
                <a href="../Compiler Options.html">
            
                    
                    编译选项
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../Configuring Watch.html">
            
                <a href="../Configuring Watch.html">
            
                    
                    配置 Watch
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="../Compiler Options in MSBuild.html">
            
                <a href="../Compiler Options in MSBuild.html">
            
                    
                    在MSBuild里使用编译选项
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.7" data-path="../Integrating with Build Tools.html">
            
                <a href="../Integrating with Build Tools.html">
            
                    
                    与其它构建工具整合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.8" data-path="../Nightly Builds.html">
            
                <a href="../Nightly Builds.html">
            
                    
                    使用TypeScript的每日构建版本
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../../wiki/">
            
                <a href="../../wiki/">
            
                    
                    Wiki
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../../wiki/this-in-TypeScript.html">
            
                <a href="../../wiki/this-in-TypeScript.html">
            
                    
                    TypeScript里的this
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../../wiki/coding_guidelines.html">
            
                <a href="../../wiki/coding_guidelines.html">
            
                    
                    编码规范
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../../wiki/Common Errors.html">
            
                <a href="../../wiki/Common Errors.html">
            
                    
                    常见编译错误
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="../../wiki/TypeScript-Editor-Support.html">
            
                <a href="../../wiki/TypeScript-Editor-Support.html">
            
                    
                    支持TypeScript的编辑器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="../../wiki/Using-TypeScript-With-ASP.NET-5.html">
            
                <a href="../../wiki/Using-TypeScript-With-ASP.NET-5.html">
            
                    
                    结合ASP.NET v5使用TypeScript
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" data-path="../../wiki/Architectural-Overview.html">
            
                <a href="../../wiki/Architectural-Overview.html">
            
                    
                    架构概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" data-path="../../wiki/Roadmap.html">
            
                <a href="../../wiki/Roadmap.html">
            
                    
                    发展路线图
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../../release-notes/">
            
                <a href="../../release-notes/">
            
                    
                    新增功能
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../../release-notes/TypeScript 3.1.html">
            
                <a href="../../release-notes/TypeScript 3.1.html">
            
                    
                    TypeScript 3.1
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../../release-notes/TypeScript 3.0.html">
            
                <a href="../../release-notes/TypeScript 3.0.html">
            
                    
                    TypeScript 3.0
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../../release-notes/TypeScript 2.9.html">
            
                <a href="../../release-notes/TypeScript 2.9.html">
            
                    
                    TypeScript 2.9
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../../release-notes/TypeScript 2.8.html">
            
                <a href="../../release-notes/TypeScript 2.8.html">
            
                    
                    TypeScript 2.8
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.5" data-path="../../release-notes/TypeScript 2.7.html">
            
                <a href="../../release-notes/TypeScript 2.7.html">
            
                    
                    TypeScript 2.7
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.6" data-path="../../release-notes/TypeScript 2.6.html">
            
                <a href="../../release-notes/TypeScript 2.6.html">
            
                    
                    TypeScript 2.6
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.7" data-path="../../release-notes/TypeScript 2.5.html">
            
                <a href="../../release-notes/TypeScript 2.5.html">
            
                    
                    TypeScript 2.5
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.8" data-path="../../release-notes/TypeScript 2.4.html">
            
                <a href="../../release-notes/TypeScript 2.4.html">
            
                    
                    TypeScript 2.4
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.9" data-path="../../release-notes/TypeScript 2.3.html">
            
                <a href="../../release-notes/TypeScript 2.3.html">
            
                    
                    TypeScript 2.3
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.10" data-path="../../release-notes/TypeScript 2.2.html">
            
                <a href="../../release-notes/TypeScript 2.2.html">
            
                    
                    TypeScript 2.2
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.11" data-path="../../release-notes/TypeScript 2.1.html">
            
                <a href="../../release-notes/TypeScript 2.1.html">
            
                    
                    TypeScript 2.1
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.12" data-path="../../release-notes/TypeScript 2.0.html">
            
                <a href="../../release-notes/TypeScript 2.0.html">
            
                    
                    TypeScript 2.0
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.13" data-path="../../release-notes/TypeScript 1.8.html">
            
                <a href="../../release-notes/TypeScript 1.8.html">
            
                    
                    TypeScript 1.8
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.14" data-path="../../release-notes/TypeScript 1.7.html">
            
                <a href="../../release-notes/TypeScript 1.7.html">
            
                    
                    TypeScript 1.7
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.15" data-path="../../release-notes/TypeScript 1.6.html">
            
                <a href="../../release-notes/TypeScript 1.6.html">
            
                    
                    TypeScript 1.6
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.16" data-path="../../release-notes/TypeScript 1.5.html">
            
                <a href="../../release-notes/TypeScript 1.5.html">
            
                    
                    TypeScript 1.5
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.17" data-path="../../release-notes/TypeScript 1.4.html">
            
                <a href="../../release-notes/TypeScript 1.4.html">
            
                    
                    TypeScript 1.4
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.18" data-path="../../release-notes/TypeScript 1.3.html">
            
                <a href="../../release-notes/TypeScript 1.3.html">
            
                    
                    TypeScript 1.3
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.19" data-path="../../release-notes/TypeScript 1.1.html">
            
                <a href="../../release-notes/TypeScript 1.1.html">
            
                    
                    TypeScript 1.1
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../../breaking-changes/breaking-changes.html">
            
                <a href="../../breaking-changes/breaking-changes.html">
            
                    
                    Breaking Changes
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../../breaking-changes/TypeScript 2.4.html">
            
                <a href="../../breaking-changes/TypeScript 2.4.html">
            
                    
                    TypeScript 2.4
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../../breaking-changes/TypeScript 2.3.html">
            
                <a href="../../breaking-changes/TypeScript 2.3.html">
            
                    
                    TypeScript 2.3
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../../breaking-changes/TypeScript 2.2.html">
            
                <a href="../../breaking-changes/TypeScript 2.2.html">
            
                    
                    TypeScript 2.2
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.4" data-path="../../breaking-changes/TypeScript 2.1.html">
            
                <a href="../../breaking-changes/TypeScript 2.1.html">
            
                    
                    TypeScript 2.1
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.5" data-path="../../breaking-changes/TypeScript 2.0.html">
            
                <a href="../../breaking-changes/TypeScript 2.0.html">
            
                    
                    TypeScript 2.0
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.6" data-path="../../breaking-changes/TypeScript 1.8.html">
            
                <a href="../../breaking-changes/TypeScript 1.8.html">
            
                    
                    TypeScript 1.8
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.7" data-path="../../breaking-changes/TypeScript 1.7.html">
            
                <a href="../../breaking-changes/TypeScript 1.7.html">
            
                    
                    TypeScript 1.7
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.8" data-path="../../breaking-changes/TypeScript 1.6.html">
            
                <a href="../../breaking-changes/TypeScript 1.6.html">
            
                    
                    TypeScript 1.6
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.9" data-path="../../breaking-changes/TypeScript 1.5.html">
            
                <a href="../../breaking-changes/TypeScript 1.5.html">
            
                    
                    TypeScript 1.5
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.10" data-path="../../breaking-changes/TypeScript 1.4.html">
            
                <a href="../../breaking-changes/TypeScript 1.4.html">
            
                    
                    TypeScript 1.4
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../.." >从JavaScript迁移到TypeScript</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <p>TypeScript&#x4E0D;&#x662F;&#x5B58;&#x5728;&#x4E8E;&#x771F;&#x7A7A;&#x4E2D;&#x3002;
&#x5B83;&#x4ECE;JavaScript&#x751F;&#x6001;&#x7CFB;&#x7EDF;&#x548C;&#x5927;&#x91CF;&#x73B0;&#x5B58;&#x7684;JavaScript&#x800C;&#x6765;&#x3002;
&#x5C06;JavaScript&#x4EE3;&#x7801;&#x8F6C;&#x6362;&#x6210;TypeScript&#x867D;&#x4E4F;&#x5473;&#x5374;&#x4E0D;&#x662F;&#x96BE;&#x4E8B;&#x3002;
&#x63A5;&#x4E0B;&#x6765;&#x8FD9;&#x7BC7;&#x6559;&#x7A0B;&#x5C06;&#x6559;&#x4F60;&#x600E;&#x4E48;&#x505A;&#x3002;
&#x5728;&#x5F00;&#x59CB;&#x8F6C;&#x6362;TypeScript&#x4E4B;&#x524D;&#xFF0C;&#x6211;&#x4EEC;&#x5047;&#x8BBE;&#x4F60;&#x5DF2;&#x7ECF;&#x7406;&#x89E3;&#x4E86;&#x8DB3;&#x591F;&#x591A;&#x672C;&#x624B;&#x518C;&#x91CC;&#x7684;&#x5185;&#x5BB9;&#x3002;</p>
<p>&#x5982;&#x679C;&#x4F60;&#x6253;&#x7B97;&#x8981;&#x8F6C;&#x6362;&#x4E00;&#x4E2A;React&#x5DE5;&#x7A0B;&#xFF0C;&#x63A8;&#x8350;&#x4F60;&#x5148;&#x9605;&#x8BFB;<a href="https://github.com/Microsoft/TypeScript-React-Conversion-Guide#typescript-react-conversion-guide" target="_blank">React&#x8F6C;&#x6362;&#x6307;&#x5357;</a>&#x3002;</p>
<h1 id="&#x8BBE;&#x7F6E;&#x76EE;&#x5F55;">&#x8BBE;&#x7F6E;&#x76EE;&#x5F55;</h1>
<p>&#x5982;&#x679C;&#x4F60;&#x5728;&#x5199;&#x7EAF;JavaScript&#xFF0C;&#x4F60;&#x5927;&#x6982;&#x662F;&#x60F3;&#x76F4;&#x63A5;&#x8FD0;&#x884C;&#x8FD9;&#x4E9B;JavaScript&#x6587;&#x4EF6;&#xFF0C;
&#x8FD9;&#x4E9B;&#x6587;&#x4EF6;&#x5B58;&#x5728;&#x4E8E;<code>src</code>&#xFF0C;<code>lib</code>&#x6216;<code>dist</code>&#x76EE;&#x5F55;&#x91CC;&#xFF0C;&#x5B83;&#x4EEC;&#x53EF;&#x4EE5;&#x6309;&#x7167;&#x9884;&#x60F3;&#x8FD0;&#x884C;&#x3002;</p>
<p>&#x82E5;&#x5982;&#x6B64;&#xFF0C;&#x90A3;&#x4E48;&#x4F60;&#x5199;&#x7684;&#x7EAF;JavaScript&#x6587;&#x4EF6;&#x5C06;&#x505A;&#x4E3A;TypeScript&#x7684;&#x8F93;&#x5165;&#xFF0C;&#x4F60;&#x5C06;&#x8981;&#x8FD0;&#x884C;&#x7684;&#x662F;TypeScript&#x7684;&#x8F93;&#x51FA;&#x3002;
&#x5728;&#x4ECE;JS&#x5230;TS&#x7684;&#x8F6C;&#x6362;&#x8FC7;&#x7A0B;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x4F1A;&#x5206;&#x79BB;&#x8F93;&#x5165;&#x6587;&#x4EF6;&#x4EE5;&#x9632;TypeScript&#x8986;&#x76D6;&#x5B83;&#x4EEC;&#x3002;
&#x4F60;&#x4E5F;&#x53EF;&#x4EE5;&#x6307;&#x5B9A;&#x8F93;&#x51FA;&#x76EE;&#x5F55;&#x3002;</p>
<p>&#x4F60;&#x53EF;&#x80FD;&#x8FD8;&#x9700;&#x8981;&#x5BF9;JavaScript&#x505A;&#x4E00;&#x4E9B;&#x4E2D;&#x95F4;&#x5904;&#x7406;&#xFF0C;&#x6BD4;&#x5982;&#x5408;&#x5E76;&#x6216;&#x7ECF;&#x8FC7;Babel&#x518D;&#x6B21;&#x7F16;&#x8BD1;&#x3002;
&#x5728;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x4F60;&#x5E94;&#x8BE5;&#x5DF2;&#x7ECF;&#x6709;&#x4E86;&#x5982;&#x4E0B;&#x7684;&#x76EE;&#x5F55;&#x7ED3;&#x6784;&#x3002;</p>
<p>&#x90A3;&#x4E48;&#x73B0;&#x5728;&#xFF0C;&#x6211;&#x4EEC;&#x5047;&#x8BBE;&#x4F60;&#x5DF2;&#x7ECF;&#x8BBE;&#x7F6E;&#x4E86;&#x8FD9;&#x6837;&#x7684;&#x76EE;&#x5F55;&#x7ED3;&#x6784;&#xFF1A;</p>
<pre><code class="lang-text">projectRoot
&#x251C;&#x2500;&#x2500; src
&#x2502;   &#x251C;&#x2500;&#x2500; file1.js
&#x2502;   &#x2514;&#x2500;&#x2500; file2.js
&#x251C;&#x2500;&#x2500; built
&#x2514;&#x2500;&#x2500; tsconfig.json
</code></pre>
<p>&#x5982;&#x679C;&#x4F60;&#x5728;<code>src</code>&#x76EE;&#x5F55;&#x5916;&#x8FD8;&#x6709;<code>tests</code>&#x6587;&#x4EF6;&#x5939;&#xFF0C;&#x90A3;&#x4E48;&#x5728;<code>src</code>&#x91CC;&#x53EF;&#x4EE5;&#x6709;&#x4E00;&#x4E2A;<code>tsconfig.json</code>&#x6587;&#x4EF6;&#xFF0C;&#x5728;<code>tests</code>&#x91CC;&#x8FD8;&#x53EF;&#x4EE5;&#x6709;&#x4E00;&#x4E2A;&#x3002;</p>
<h1 id="&#x4E66;&#x5199;&#x914D;&#x7F6E;&#x6587;&#x4EF6;">&#x4E66;&#x5199;&#x914D;&#x7F6E;&#x6587;&#x4EF6;</h1>
<p>TypeScript&#x4F7F;&#x7528;<code>tsconfig.json</code>&#x6587;&#x4EF6;&#x7BA1;&#x7406;&#x5DE5;&#x7A0B;&#x914D;&#x7F6E;&#xFF0C;&#x4F8B;&#x5982;&#x4F60;&#x60F3;&#x5305;&#x542B;&#x54EA;&#x4E9B;&#x6587;&#x4EF6;&#x548C;&#x8FDB;&#x884C;&#x54EA;&#x4E9B;&#x68C0;&#x67E5;&#x3002;
&#x8BA9;&#x6211;&#x4EEC;&#x5148;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x5DE5;&#x7A0B;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#xFF1A;</p>
<pre><code class="lang-json">{
    <span class="hljs-string">&quot;compilerOptions&quot;</span>: {
        <span class="hljs-string">&quot;outDir&quot;</span>: <span class="hljs-string">&quot;./built&quot;</span>,
        <span class="hljs-string">&quot;allowJs&quot;</span>: <span class="hljs-literal">true</span>,
        <span class="hljs-string">&quot;target&quot;</span>: <span class="hljs-string">&quot;es5&quot;</span>
    },
    <span class="hljs-string">&quot;include&quot;</span>: [
        <span class="hljs-string">&quot;./src/**/*&quot;</span>
    ]
}
</code></pre>
<p>&#x8FD9;&#x91CC;&#x6211;&#x4EEC;&#x4E3A;TypeScript&#x8BBE;&#x7F6E;&#x4E86;&#x4E00;&#x4E9B;&#x4E1C;&#x897F;:</p>
<ol>
<li>&#x8BFB;&#x53D6;&#x6240;&#x6709;&#x53EF;&#x8BC6;&#x522B;&#x7684;<code>src</code>&#x76EE;&#x5F55;&#x4E0B;&#x7684;&#x6587;&#x4EF6;&#xFF08;&#x901A;&#x8FC7;<code>include</code>&#xFF09;&#x3002;</li>
<li>&#x63A5;&#x53D7;JavaScript&#x505A;&#x4E3A;&#x8F93;&#x5165;&#xFF08;&#x901A;&#x8FC7;<code>allowJs</code>&#xFF09;&#x3002;</li>
<li>&#x751F;&#x6210;&#x7684;&#x6240;&#x6709;&#x6587;&#x4EF6;&#x653E;&#x5728;<code>built</code>&#x76EE;&#x5F55;&#x4E0B;&#xFF08;&#x901A;&#x8FC7;<code>outDir</code>&#xFF09;&#x3002;</li>
<li>&#x5C06;JavaScript&#x4EE3;&#x7801;&#x964D;&#x7EA7;&#x5230;&#x4F4E;&#x7248;&#x672C;&#x6BD4;&#x5982;ECMAScript 5&#xFF08;&#x901A;&#x8FC7;<code>target</code>&#xFF09;&#x3002;</li>
</ol>
<p>&#x73B0;&#x5728;&#xFF0C;&#x5982;&#x679C;&#x4F60;&#x5728;&#x5DE5;&#x7A0B;&#x6839;&#x76EE;&#x5F55;&#x4E0B;&#x8FD0;&#x884C;<code>tsc</code>&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x5728;<code>built</code>&#x76EE;&#x5F55;&#x4E0B;&#x770B;&#x5230;&#x751F;&#x6210;&#x7684;&#x6587;&#x4EF6;&#x3002;
<code>built</code>&#x4E0B;&#x7684;&#x6587;&#x4EF6;&#x5E94;&#x8BE5;&#x4E0E;<code>src</code>&#x4E0B;&#x7684;&#x6587;&#x4EF6;&#x76F8;&#x540C;&#x3002;
&#x73B0;&#x5728;&#x4F60;&#x7684;&#x5DE5;&#x7A0B;&#x91CC;&#x7684;TypeScript&#x5DF2;&#x7ECF;&#x53EF;&#x4EE5;&#x5DE5;&#x4F5C;&#x4E86;&#x3002;</p>
<h2 id="&#x65E9;&#x671F;&#x6536;&#x76CA;">&#x65E9;&#x671F;&#x6536;&#x76CA;</h2>
<p>&#x73B0;&#x5728;&#x4F60;&#x5DF2;&#x7ECF;&#x53EF;&#x4EE5;&#x770B;&#x5230;TypeScript&#x5E26;&#x6765;&#x7684;&#x597D;&#x5904;&#xFF0C;&#x5B83;&#x80FD;&#x5E2E;&#x52A9;&#x6211;&#x4EEC;&#x7406;&#x89E3;&#x5F53;&#x524D;&#x5DE5;&#x7A0B;&#x3002;
&#x5982;&#x679C;&#x4F60;&#x6253;&#x5F00;&#x50CF;<a href="https://code.visualstudio.com" target="_blank">VS Code</a>&#x6216;<a href="https://visualstudio.com" target="_blank">Visual Studio</a>&#x8FD9;&#x6837;&#x7684;&#x7F16;&#x8BD1;&#x5668;&#xFF0C;&#x4F60;&#x5C31;&#x80FD;&#x4F7F;&#x7528;&#x50CF;&#x81EA;&#x52A8;&#x8865;&#x5168;&#x8FD9;&#x6837;&#x7684;&#x5DE5;&#x5177;&#x3002;
&#x4F60;&#x8FD8;&#x53EF;&#x4EE5;&#x914D;&#x7F6E;&#x5982;&#x4E0B;&#x7684;&#x9009;&#x9879;&#x6765;&#x5E2E;&#x52A9;&#x67E5;&#x627E;BUG&#xFF1A;</p>
<ul>
<li><code>noImplicitReturns</code> &#x4F1A;&#x9632;&#x6B62;&#x4F60;&#x5FD8;&#x8BB0;&#x5728;&#x51FD;&#x6570;&#x672B;&#x5C3E;&#x8FD4;&#x56DE;&#x503C;&#x3002;</li>
<li><code>noFallthroughCasesInSwitch</code> &#x4F1A;&#x9632;&#x6B62;&#x5728;<code>switch</code>&#x4EE3;&#x7801;&#x5757;&#x91CC;&#x7684;&#x4E24;&#x4E2A;<code>case</code>&#x4E4B;&#x95F4;&#x5FD8;&#x8BB0;&#x6DFB;&#x52A0;<code>break</code>&#x8BED;&#x53E5;&#x3002;</li>
</ul>
<p>TypeScript&#x8FD8;&#x80FD;&#x53D1;&#x73B0;&#x90A3;&#x4E9B;&#x6267;&#x884C;&#x4E0D;&#x5230;&#x7684;&#x4EE3;&#x7801;&#x548C;&#x6807;&#x7B7E;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x8BBE;&#x7F6E;<code>allowUnreachableCode</code>&#x548C;<code>allowUnusedLabels</code>&#x9009;&#x9879;&#x6765;&#x7981;&#x7528;&#x3002;</p>
<h1 id="&#x4E0E;&#x6784;&#x5EFA;&#x5DE5;&#x5177;&#x8FDB;&#x884C;&#x96C6;&#x6210;">&#x4E0E;&#x6784;&#x5EFA;&#x5DE5;&#x5177;&#x8FDB;&#x884C;&#x96C6;&#x6210;</h1>
<p>&#x5728;&#x4F60;&#x7684;&#x6784;&#x5EFA;&#x7BA1;&#x9053;&#x4E2D;&#x53EF;&#x80FD;&#x5305;&#x542B;&#x591A;&#x4E2A;&#x6B65;&#x9AA4;&#x3002;
&#x6BD4;&#x5982;&#x4E3A;&#x6BCF;&#x4E2A;&#x6587;&#x4EF6;&#x6DFB;&#x52A0;&#x4E00;&#x4E9B;&#x5185;&#x5BB9;&#x3002;
&#x6BCF;&#x79CD;&#x5DE5;&#x5177;&#x7684;&#x4F7F;&#x7528;&#x65B9;&#x6CD5;&#x90FD;&#x662F;&#x4E0D;&#x540C;&#x7684;&#xFF0C;&#x6211;&#x4EEC;&#x4F1A;&#x5C3D;&#x53EF;&#x80FD;&#x7684;&#x5305;&#x6DB5;&#x4E3B;&#x6D41;&#x7684;&#x5DE5;&#x5177;&#x3002;</p>
<h2 id="gulp">Gulp</h2>
<p>&#x5982;&#x679C;&#x4F60;&#x5728;&#x4F7F;&#x7528;&#x65F6;&#x9AE6;&#x7684;Gulp&#xFF0C;&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x6709;&#x4E00;&#x7BC7;&#x5173;&#x4E8E;<a href="Gulp.html">&#x4F7F;&#x7528;Gulp</a>&#x7ED3;&#x5408;TypeScript&#x5E76;&#x4E0E;&#x5E38;&#x89C1;&#x6784;&#x5EFA;&#x5DE5;&#x5177;Browserify&#xFF0C;Babelify&#x548C;Uglify&#x8FDB;&#x884C;&#x96C6;&#x6210;&#x7684;&#x6559;&#x7A0B;&#x3002;
&#x8BF7;&#x9605;&#x8BFB;&#x8FD9;&#x7BC7;&#x6559;&#x7A0B;&#x3002;</p>
<h2 id="webpack">Webpack</h2>
<p>Webpack&#x96C6;&#x6210;&#x975E;&#x5E38;&#x7B80;&#x5355;&#x3002;
&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>awesome-typescript-loader</code>&#xFF0C;&#x5B83;&#x662F;&#x4E00;&#x4E2A;TypeScript&#x7684;&#x52A0;&#x8F7D;&#x5668;&#xFF0C;&#x7ED3;&#x5408;<code>source-map-loader</code>&#x65B9;&#x4FBF;&#x8C03;&#x8BD5;&#x3002;
&#x8FD0;&#x884C;&#xFF1A;</p>
<pre><code class="lang-shell">npm install awesome-typescript-loader source-map-loader
</code></pre>
<p>&#x5E76;&#x5C06;&#x4E0B;&#x9762;&#x7684;&#x9009;&#x9879;&#x5408;&#x5E76;&#x5230;&#x4F60;&#x7684;<code>webpack.config.js</code>&#x6587;&#x4EF6;&#x91CC;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-built_in">module</span>.exports = {
    entry: <span class="hljs-string">&quot;./src/index.ts&quot;</span>,
    output: {
        filename: <span class="hljs-string">&quot;./dist/bundle.js&quot;</span>,
    },

    <span class="hljs-comment">// Enable sourcemaps for debugging webpack&apos;s output.</span>
    devtool: <span class="hljs-string">&quot;source-map&quot;</span>,

    resolve: {
        <span class="hljs-comment">// Add &apos;.ts&apos; and &apos;.tsx&apos; as resolvable extensions.</span>
        extensions: [<span class="hljs-string">&quot;&quot;</span>, <span class="hljs-string">&quot;.webpack.js&quot;</span>, <span class="hljs-string">&quot;.web.js&quot;</span>, <span class="hljs-string">&quot;.ts&quot;</span>, <span class="hljs-string">&quot;.tsx&quot;</span>, <span class="hljs-string">&quot;.js&quot;</span>]
    },

    <span class="hljs-built_in">module</span>: {
        loaders: [
            <span class="hljs-comment">// All files with a &apos;.ts&apos; or &apos;.tsx&apos; extension will be handled by &apos;awesome-typescript-loader&apos;.</span>
            { test: <span class="hljs-regexp">/\.tsx?$/</span>, loader: <span class="hljs-string">&quot;awesome-typescript-loader&quot;</span> }
        ],

        preLoaders: [
            <span class="hljs-comment">// All output &apos;.js&apos; files will have any sourcemaps re-processed by &apos;source-map-loader&apos;.</span>
            { test: <span class="hljs-regexp">/\.js$/</span>, loader: <span class="hljs-string">&quot;source-map-loader&quot;</span> }
        ]
    },

    <span class="hljs-comment">// Other options...</span>
};
</code></pre>
<p>&#x8981;&#x6CE8;&#x610F;&#x7684;&#x662F;&#xFF0C;<code>awesome-typescript-loader</code>&#x5FC5;&#x987B;&#x5728;&#x5176;&#x5B83;&#x5904;&#x7406;<code>.js</code>&#x6587;&#x4EF6;&#x7684;&#x52A0;&#x8F7D;&#x5668;&#x4E4B;&#x524D;&#x8FD0;&#x884C;&#x3002;</p>
<p>&#x8FD9;&#x4E0E;&#x53E6;&#x4E00;&#x4E2A;TypeScript&#x7684;Webpack&#x52A0;&#x8F7D;&#x5668;<a href="https://github.com/TypeStrong/ts-loader" target="_blank">ts-loader</a>&#x662F;&#x4E00;&#x6837;&#x7684;&#x3002;
&#x4F60;&#x53EF;&#x4EE5;&#x5230;<a href="https://github.com/s-panferov/awesome-typescript-loader#differences-between-ts-loader" target="_blank">&#x8FD9;&#x91CC;</a>&#x4E86;&#x89E3;&#x4E24;&#x8005;&#x4E4B;&#x95F4;&#x7684;&#x5DEE;&#x522B;&#x3002;</p>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x5728;<a href="React%20&amp;%20Webpack.md">React&#x548C;Webpack&#x6559;&#x7A0B;</a>&#x91CC;&#x627E;&#x5230;&#x4F7F;&#x7528;Webpack&#x7684;&#x4F8B;&#x5B50;&#x3002;</p>
<h1 id="&#x8F6C;&#x6362;&#x5230;typescript&#x6587;&#x4EF6;">&#x8F6C;&#x6362;&#x5230;TypeScript&#x6587;&#x4EF6;</h1>
<p>&#x5230;&#x76EE;&#x524D;&#x4E3A;&#x6B62;&#xFF0C;&#x4F60;&#x5DF2;&#x7ECF;&#x505A;&#x597D;&#x4E86;&#x4F7F;&#x7528;TypeScript&#x6587;&#x4EF6;&#x7684;&#x51C6;&#x5907;&#x3002;
&#x7B2C;&#x4E00;&#x6B65;&#xFF0C;&#x5C06;<code>.js</code>&#x6587;&#x4EF6;&#x91CD;&#x547D;&#x540D;&#x4E3A;<code>.ts</code>&#x6587;&#x4EF6;&#x3002;
&#x5982;&#x679C;&#x4F60;&#x4F7F;&#x7528;&#x4E86;JSX&#xFF0C;&#x5219;&#x91CD;&#x547D;&#x540D;&#x4E3A;<code>.tsx</code>&#x6587;&#x4EF6;&#x3002;</p>
<p>&#x7B2C;&#x4E00;&#x6B65;&#x8FBE;&#x6210;&#xFF1F;
&#x592A;&#x68D2;&#x4E86;!
&#x4F60;&#x5DF2;&#x7ECF;&#x6210;&#x529F;&#x5730;&#x5C06;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;&#x4ECE;JavaScript&#x8F6C;&#x6362;&#x6210;&#x4E86;TypeScript!</p>
<p>&#x5F53;&#x7136;&#x4E86;&#xFF0C;&#x4F60;&#x53EF;&#x80FD;&#x611F;&#x89C9;&#x54EA;&#x91CC;&#x4E0D;&#x5BF9;&#x52B2;&#x513F;&#x3002;
&#x5982;&#x679C;&#x4F60;&#x5728;&#x652F;&#x6301;TypeScript&#x7684;&#x7F16;&#x8F91;&#x5668;&#xFF08;&#x6216;&#x8FD0;&#x884C;<code>tsc --pretty</code>&#xFF09;&#x91CC;&#x6253;&#x5F00;&#x4E86;&#x90A3;&#x4E2A;&#x6587;&#x4EF6;&#xFF0C;&#x4F60;&#x53EF;&#x80FD;&#x4F1A;&#x770B;&#x5230;&#x6709;&#x4E9B;&#x884C;&#x4E0A;&#x6709;&#x7EA2;&#x8272;&#x7684;&#x6CE2;&#x6D6A;&#x7EBF;&#x3002;
&#x4F60;&#x53EF;&#x4EE5;&#x628A;&#x5B83;&#x4EEC;&#x5F53;&#x505A;&#x5728;Microsoft Word&#x91CC;&#x770B;&#x5230;&#x7684;&#x7EA2;&#x8272;&#x6CE2;&#x6D6A;&#x7EBF;&#x4E00;&#x6837;&#x3002;
&#x4F46;&#x662F;TypeScript&#x4ECD;&#x7136;&#x4F1A;&#x7F16;&#x8BD1;&#x4F60;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x5C31;&#x597D;&#x6BD4;Word&#x8FD8;&#x662F;&#x5141;&#x8BB8;&#x4F60;&#x6253;&#x5370;&#x4F60;&#x7684;&#x6587;&#x6863;&#x4E00;&#x6837;&#x3002;</p>
<p>&#x5982;&#x679C;&#x5BF9;&#x4F60;&#x6765;&#x8BF4;&#x8FD9;&#x79CD;&#x884C;&#x4E3A;&#x592A;&#x968F;&#x4FBF;&#x4E86;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x8BA9;&#x5B83;&#x53D8;&#x5F97;&#x4E25;&#x683C;&#x4E9B;&#x3002;
&#x5982;&#x679C;&#xFF0C;&#x4F60;<em>&#x4E0D;&#x60F3;</em>&#x5728;&#x53D1;&#x751F;&#x9519;&#x8BEF;&#x7684;&#x65F6;&#x5019;&#xFF0C;TypeScript&#x8FD8;&#x4F1A;&#x88AB;&#x7F16;&#x8BD1;&#x6210;JavaScript&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>noEmitOnError</code>&#x9009;&#x9879;&#x3002;
&#x4ECE;&#x67D0;&#x79CD;&#x610F;&#x4E49;&#x4E0A;&#x6765;&#x8BB2;&#xFF0C;TypeScript&#x5177;&#x6709;&#x4E00;&#x4E2A;&#x8C03;&#x6574;&#x5B83;&#x7684;&#x4E25;&#x683C;&#x6027;&#x7684;&#x523B;&#x5EA6;&#x76D8;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x5C06;&#x6307;&#x9488;&#x62D4;&#x52A8;&#x5230;&#x4F60;&#x60F3;&#x8981;&#x7684;&#x4F4D;&#x7F6E;&#x3002;</p>
<p>&#x5982;&#x679C;&#x4F60;&#x8BA1;&#x5212;&#x4F7F;&#x7528;&#x53EF;&#x7528;&#x7684;&#x9AD8;&#x5EA6;&#x4E25;&#x683C;&#x7684;&#x8BBE;&#x7F6E;&#xFF0C;&#x6700;&#x597D;&#x73B0;&#x5728;&#x5C31;&#x542F;&#x7528;&#x5B83;&#x4EEC;&#xFF08;&#x67E5;&#x770B;<a href="#getting-stricter-checks">&#x542F;&#x7528;&#x4E25;&#x683C;&#x68C0;&#x67E5;</a>&#xFF09;&#x3002;
&#x6BD4;&#x5982;&#xFF0C;&#x5982;&#x679C;&#x4F60;&#x4E0D;&#x60F3;&#x8BA9;TypeScript&#x5C06;&#x6CA1;&#x6709;&#x660E;&#x786E;&#x6307;&#x5B9A;&#x7684;&#x7C7B;&#x578B;&#x9ED8;&#x9ED8;&#x5730;&#x63A8;&#x65AD;&#x4E3A;<code>any</code>&#x7C7B;&#x578B;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x4FEE;&#x6539;&#x6587;&#x4EF6;&#x4E4B;&#x524D;&#x542F;&#x7528;<code>noImplicitAny</code>&#x3002;
&#x4F60;&#x53EF;&#x80FD;&#x4F1A;&#x89C9;&#x5F97;&#x8FD9;&#x6709;&#x4E9B;&#x8FC7;&#x5EA6;&#x4E25;&#x683C;&#xFF0C;&#x4F46;&#x662F;&#x957F;&#x671F;&#x6536;&#x76CA;&#x5F88;&#x5FEB;&#x5C31;&#x80FD;&#x663E;&#x73B0;&#x51FA;&#x6765;&#x3002;</p>
<h2 id="&#x53BB;&#x9664;&#x9519;&#x8BEF;">&#x53BB;&#x9664;&#x9519;&#x8BEF;</h2>
<p>&#x6211;&#x4EEC;&#x63D0;&#x5230;&#x8FC7;&#xFF0C;&#x82E5;&#x4E0D;&#x51FA;&#x6240;&#x6599;&#xFF0C;&#x5728;&#x8F6C;&#x6362;&#x540E;&#x5C06;&#x4F1A;&#x770B;&#x5230;&#x9519;&#x8BEF;&#x4FE1;&#x606F;&#x3002;
&#x91CD;&#x8981;&#x7684;&#x662F;&#x6211;&#x4EEC;&#x8981;&#x9010;&#x4E00;&#x7684;&#x67E5;&#x770B;&#x5B83;&#x4EEC;&#x5E76;&#x51B3;&#x5B9A;&#x5982;&#x4F55;&#x5904;&#x7406;&#x3002;
&#x901A;&#x5E38;&#x8FD9;&#x4E9B;&#x90FD;&#x662F;&#x771F;&#x6B63;&#x7684;BUG&#xFF0C;&#x4F46;&#x6709;&#x65F6;&#x5FC5;&#x987B;&#x8981;&#x544A;&#x8BC9;TypeScript&#x4F60;&#x8981;&#x505A;&#x7684;&#x662F;&#x4EC0;&#x4E48;&#x3002;</p>
<h3 id="&#x7531;&#x6A21;&#x5757;&#x5BFC;&#x5165;">&#x7531;&#x6A21;&#x5757;&#x5BFC;&#x5165;</h3>
<p>&#x9996;&#x5148;&#x4F60;&#x53EF;&#x80FD;&#x4F1A;&#x770B;&#x5230;&#x4E00;&#x4E9B;&#x7C7B;&#x4F3C;<code>Cannot find name &apos;require&apos;.</code>&#x548C;<code>Cannot find name &apos;define&apos;.</code>&#x7684;&#x9519;&#x8BEF;&#x3002;
&#x9047;&#x5230;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x8BF4;&#x660E;&#x4F60;&#x5728;&#x4F7F;&#x7528;&#x6A21;&#x5757;&#x3002;
&#x4F60;&#x4EC5;&#x9700;&#x8981;&#x544A;&#x8BC9;TypeScript&#x5B83;&#x4EEC;&#x662F;&#x5B58;&#x5728;&#x7684;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-comment">// For Node/CommonJS</span>
<span class="hljs-keyword">declare</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">require</span>(<span class="hljs-params">path: <span class="hljs-built_in">string</span></span>): <span class="hljs-title">any</span></span>;
</code></pre>
<p>&#x6216;</p>
<pre><code class="lang-ts"><span class="hljs-comment">// For RequireJS/AMD</span>
<span class="hljs-keyword">declare</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">define</span>(<span class="hljs-params">...args: <span class="hljs-built_in">any</span>[]</span>): <span class="hljs-title">any</span></span>;
</code></pre>
<p>&#x6700;&#x597D;&#x662F;&#x907F;&#x514D;&#x4F7F;&#x7528;&#x8FD9;&#x4E9B;&#x8C03;&#x7528;&#x800C;&#x6539;&#x7528;TypeScript&#x7684;&#x5BFC;&#x5165;&#x8BED;&#x6CD5;&#x3002;</p>
<p>&#x9996;&#x5148;&#xFF0C;&#x4F60;&#x8981;&#x4F7F;&#x7528;TypeScript&#x7684;<code>module</code>&#x6807;&#x8BB0;&#x6765;&#x542F;&#x7528;&#x4E00;&#x4E9B;&#x6A21;&#x5757;&#x7CFB;&#x7EDF;&#x3002;
&#x53EF;&#x7528;&#x7684;&#x9009;&#x9879;&#x6709;<code>commonjs</code>&#xFF0C;<code>amd</code>&#xFF0C;<code>system</code>&#xFF0C;and <code>umd</code>&#x3002;</p>
<p>&#x5982;&#x679C;&#x4EE3;&#x7801;&#x91CC;&#x5B58;&#x5728;&#x4E0B;&#x9762;&#x7684;Node/CommonJS&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> foo = <span class="hljs-built_in">require</span>(<span class="hljs-string">&quot;foo&quot;</span>);

foo.doStuff();
</code></pre>
<p>&#x6216;&#x8005;&#x4E0B;&#x9762;&#x7684;RequireJS/AMD&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-js">define([<span class="hljs-string">&quot;foo&quot;</span>], <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">foo</span>) </span>{
    foo.doStuff();
})
</code></pre>
<p>&#x90A3;&#x4E48;&#x53EF;&#x4EE5;&#x5199;&#x505A;&#x4E0B;&#x9762;&#x7684;TypeScript&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">import</span> foo = <span class="hljs-built_in">require</span>(<span class="hljs-string">&quot;foo&quot;</span>);

foo.doStuff();
</code></pre>
<h3 id="&#x83B7;&#x53D6;&#x58F0;&#x660E;&#x6587;&#x4EF6;">&#x83B7;&#x53D6;&#x58F0;&#x660E;&#x6587;&#x4EF6;</h3>
<p>&#x5982;&#x679C;&#x4F60;&#x5F00;&#x59CB;&#x505A;&#x8F6C;&#x6362;&#x5230;TypeScript&#x5BFC;&#x5165;&#xFF0C;&#x4F60;&#x53EF;&#x80FD;&#x4F1A;&#x9047;&#x5230;<code>Cannot find module &apos;foo&apos;.</code>&#x8FD9;&#x6837;&#x7684;&#x9519;&#x8BEF;&#x3002;
&#x95EE;&#x9898;&#x51FA;&#x5728;&#x6CA1;&#x6709;<em>&#x58F0;&#x660E;&#x6587;&#x4EF6;</em>&#x6765;&#x63CF;&#x8FF0;&#x4F60;&#x7684;&#x4EE3;&#x7801;&#x5E93;&#x3002;
&#x5E78;&#x8FD0;&#x7684;&#x662F;&#x8FD9;&#x975E;&#x5E38;&#x7B80;&#x5355;&#x3002;
&#x5982;&#x679C;TypeScript&#x62A5;&#x6028;&#x50CF;&#x662F;&#x6CA1;&#x6709;<code>lodash</code>&#x5305;&#xFF0C;&#x90A3;&#x4F60;&#x53EA;&#x9700;&#x8FD9;&#x6837;&#x505A;</p>
<pre><code class="lang-shell">npm install -S @types/lodash
</code></pre>
<p>&#x5982;&#x679C;&#x4F60;&#x6CA1;&#x6709;&#x4F7F;&#x7528;<code>commonjs</code>&#x6A21;&#x5757;&#x6A21;&#x5757;&#x9009;&#x9879;&#xFF0C;&#x90A3;&#x4E48;&#x5C31;&#x9700;&#x8981;&#x5C06;<code>moduleResolution</code>&#x9009;&#x9879;&#x8BBE;&#x7F6E;&#x4E3A;<code>node</code>&#x3002;</p>
<p>&#x4E4B;&#x540E;&#xFF0C;&#x4F60;&#x5E94;&#x8BE5;&#x5C31;&#x53EF;&#x4EE5;&#x5BFC;&#x5165;<code>lodash</code>&#x4E86;&#xFF0C;&#x5E76;&#x4E14;&#x4F1A;&#x83B7;&#x5F97;&#x7CBE;&#x786E;&#x7684;&#x81EA;&#x52A8;&#x8865;&#x5168;&#x529F;&#x80FD;&#x3002;</p>
<h3 id="&#x7531;&#x6A21;&#x5757;&#x5BFC;&#x51FA;">&#x7531;&#x6A21;&#x5757;&#x5BFC;&#x51FA;</h3>
<p>&#x901A;&#x5E38;&#x6765;&#x8BB2;&#xFF0C;&#x7531;&#x6A21;&#x5757;&#x5BFC;&#x51FA;&#x6D89;&#x53CA;&#x6DFB;&#x52A0;&#x5C5E;&#x6027;&#x5230;<code>exports</code>&#x6216;<code>module.exports</code>&#x3002;
TypeScript&#x5141;&#x8BB8;&#x4F60;&#x4F7F;&#x7528;&#x9876;&#x7EA7;&#x7684;&#x5BFC;&#x51FA;&#x8BED;&#x53E5;&#x3002;
&#x6BD4;&#x5982;&#xFF0C;&#x4F60;&#x8981;&#x5BFC;&#x51FA;&#x4E0B;&#x9762;&#x7684;&#x51FD;&#x6570;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-built_in">module</span>.exports.feedPets = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">pets</span>) </span>{
    <span class="hljs-comment">// ...</span>
}
</code></pre>
<p>&#x90A3;&#x4E48;&#x4F60;&#x53EF;&#x4EE5;&#x8FD9;&#x6837;&#x5199;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">feedPets</span>(<span class="hljs-params">pets</span>) </span>{
    <span class="hljs-comment">// ...</span>
}
</code></pre>
<p>&#x6709;&#x65F6;&#x4F60;&#x4F1A;&#x5B8C;&#x5168;&#x91CD;&#x5199;&#x5BFC;&#x51FA;&#x5BF9;&#x8C61;&#x3002;
&#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x5E38;&#x89C1;&#x6A21;&#x5F0F;&#xFF0C;&#x8FD9;&#x4F1A;&#x5C06;&#x6A21;&#x5757;&#x53D8;&#x4E3A;&#x53EF;&#x7ACB;&#x5373;&#x8C03;&#x7528;&#x7684;&#x6A21;&#x5757;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">&quot;express&quot;</span>);
<span class="hljs-keyword">var</span> app = express();
</code></pre>
<p>&#x4E4B;&#x524D;&#x4F60;&#x53EF;&#x4EE5;&#x662F;&#x8FD9;&#x6837;&#x5199;&#x7684;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-comment">// ...</span>
}
<span class="hljs-built_in">module</span>.exports = foo;
</code></pre>
<p>&#x5728;TypeScript&#x91CC;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>export =</code>&#x6765;&#x4EE3;&#x66FF;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-comment">// ...</span>
}
<span class="hljs-keyword">export</span> = foo;
</code></pre>
<h3 id="&#x8FC7;&#x591A;&#x6216;&#x8FC7;&#x5C11;&#x7684;&#x53C2;&#x6570;">&#x8FC7;&#x591A;&#x6216;&#x8FC7;&#x5C11;&#x7684;&#x53C2;&#x6570;</h3>
<p>&#x6709;&#x65F6;&#x4F60;&#x4F1A;&#x53D1;&#x73B0;&#x4F60;&#x5728;&#x8C03;&#x7528;&#x4E00;&#x4E2A;&#x5177;&#x6709;&#x8FC7;&#x591A;&#x6216;&#x8FC7;&#x5C11;&#x53C2;&#x6570;&#x7684;&#x51FD;&#x6570;&#x3002;
&#x901A;&#x5E38;&#xFF0C;&#x8FD9;&#x662F;&#x4E00;&#x4E2A;BUG&#xFF0C;&#x4F46;&#x5728;&#x67D0;&#x4E9B;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x58F0;&#x660E;&#x4E00;&#x4E2A;&#x4F7F;&#x7528;<code>arguments</code>&#x5BF9;&#x8C61;&#x7684;&#x51FD;&#x6570;&#x800C;&#x4E0D;&#x9700;&#x8981;&#x5199;&#x51FA;&#x6240;&#x6709;&#x53C2;&#x6570;:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">myCoolFunction</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">if</span> (<span class="hljs-built_in">arguments</span>.length == <span class="hljs-number">2</span> &amp;&amp; !<span class="hljs-built_in">Array</span>.isArray(<span class="hljs-built_in">arguments</span>[<span class="hljs-number">1</span>])) {
        <span class="hljs-keyword">var</span> f = <span class="hljs-built_in">arguments</span>[<span class="hljs-number">0</span>];
        <span class="hljs-keyword">var</span> arr = <span class="hljs-built_in">arguments</span>[<span class="hljs-number">1</span>];
        <span class="hljs-comment">// ...</span>
    }
    <span class="hljs-comment">// ...</span>
}

myCoolFunction(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">x</span>) </span>{ <span class="hljs-built_in">console</span>.log(x) }, [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>]);
myCoolFunction(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">x</span>) </span>{ <span class="hljs-built_in">console</span>.log(x) }, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>);
</code></pre>
<p>&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5229;&#x7528;TypeScript&#x7684;&#x51FD;&#x6570;&#x91CD;&#x8F7D;&#x6765;&#x544A;&#x8BC9;&#x8C03;&#x7528;&#x8005;<code>myCoolFunction</code>&#x51FD;&#x6570;&#x7684;&#x8C03;&#x7528;&#x65B9;&#x5F0F;&#x3002;</p>
<pre><code class="lang-ts">function myCoolFunction(f: (x: number) =&gt; void, nums: number[]): void;
function myCoolFunction(f: (x: number) =&gt; void, ...nums: number[]): void;
function myCoolFunction() {
    if (arguments.length == 2 &amp;&amp; !Array.isArray(arguments[1])) {
        var f = arguments[0];
        var arr = arguments[1];
        // ...
    }
    // ...
}
</code></pre>
<p>&#x6211;&#x4EEC;&#x4E3A;<code>myCoolFunction</code>&#x51FD;&#x6570;&#x6DFB;&#x52A0;&#x4E86;&#x4E24;&#x4E2A;&#x91CD;&#x8F7D;&#x7B7E;&#x540D;&#x3002;
&#x7B2C;&#x4E00;&#x4E2A;&#x68C0;&#x67E5;<code>myCoolFunction</code>&#x51FD;&#x6570;&#x662F;&#x5426;&#x63A5;&#x6536;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF08;&#x5B83;&#x53C8;&#x63A5;&#x6536;&#x4E00;&#x4E2A;<code>number</code>&#x53C2;&#x6570;&#xFF09;&#x548C;&#x4E00;&#x4E2A;<code>number</code>&#x6570;&#x7EC4;&#x3002;
&#x7B2C;&#x4E8C;&#x4E2A;&#x540C;&#x6837;&#x662F;&#x63A5;&#x6536;&#x4E86;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x5E76;&#x4E14;&#x4F7F;&#x7528;&#x5269;&#x4F59;&#x53C2;&#x6570;&#xFF08;<code>...nums</code>&#xFF09;&#x6765;&#x8868;&#x793A;&#x4E4B;&#x540E;&#x7684;&#x5176;&#x5B83;&#x6240;&#x6709;&#x53C2;&#x6570;&#x5FC5;&#x987B;&#x662F;<code>number</code>&#x7C7B;&#x578B;&#x3002;</p>
<h3 id="&#x8FDE;&#x7EED;&#x6DFB;&#x52A0;&#x5C5E;&#x6027;">&#x8FDE;&#x7EED;&#x6DFB;&#x52A0;&#x5C5E;&#x6027;</h3>
<p>&#x6709;&#x4E9B;&#x4EBA;&#x53EF;&#x80FD;&#x4F1A;&#x56E0;&#x4E3A;&#x4EE3;&#x7801;&#x7F8E;&#x89C2;&#x6027;&#x800C;&#x559C;&#x6B22;&#x5148;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x7136;&#x540E;&#x7ACB;&#x5373;&#x6DFB;&#x52A0;&#x5C5E;&#x6027;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> options = {};
options.color = <span class="hljs-string">&quot;red&quot;</span>;
options.volume = <span class="hljs-number">11</span>;
</code></pre>
<p>TypeScript&#x4F1A;&#x63D0;&#x793A;&#x4F60;&#x4E0D;&#x80FD;&#x7ED9;<code>color</code>&#x548C;<code>volumn</code>&#x8D4B;&#x503C;&#xFF0C;&#x56E0;&#x4E3A;&#x5148;&#x524D;&#x6307;&#x5B9A;<code>options</code>&#x7684;&#x7C7B;&#x578B;&#x4E3A;<code>{}</code>&#x5E76;&#x4E0D;&#x5E26;&#x6709;&#x4EFB;&#x4F55;&#x5C5E;&#x6027;&#x3002;
&#x5982;&#x679C;&#x4F60;&#x5C06;&#x58F0;&#x660E;&#x53D8;&#x6210;&#x5BF9;&#x8C61;&#x5B57;&#x9762;&#x91CF;&#x7684;&#x5F62;&#x5F0F;&#x5C06;&#x4E0D;&#x4F1A;&#x4EA7;&#x751F;&#x9519;&#x8BEF;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">let</span> options = {
    color: <span class="hljs-string">&quot;red&quot;</span>,
    volume: <span class="hljs-number">11</span>
};
</code></pre>
<p>&#x4F60;&#x8FD8;&#x53EF;&#x4EE5;&#x5B9A;&#x4E49;<code>options</code>&#x7684;&#x7C7B;&#x578B;&#x5E76;&#x4E14;&#x6DFB;&#x52A0;&#x7C7B;&#x578B;&#x65AD;&#x8A00;&#x5230;&#x5BF9;&#x8C61;&#x5B57;&#x9762;&#x91CF;&#x4E0A;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">interface</span> Options { color: <span class="hljs-built_in">string</span>; volume: <span class="hljs-built_in">number</span> }

<span class="hljs-keyword">let</span> options = {} as Options;
options.color = <span class="hljs-string">&quot;red&quot;</span>;
options.volume = <span class="hljs-number">11</span>;
</code></pre>
<p>&#x6216;&#x8005;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x5C06;<code>options</code>&#x6307;&#x5B9A;&#x6210;<code>any</code>&#x7C7B;&#x578B;&#xFF0C;&#x8FD9;&#x662F;&#x6700;&#x7B80;&#x5355;&#x7684;&#xFF0C;&#x4F46;&#x4E5F;&#x662F;&#x83B7;&#x76CA;&#x6700;&#x5C11;&#x7684;&#x3002;</p>
<h3 id="any&#xFF0C;object&#xFF0C;&#x548C;"><code>any</code>&#xFF0C;<code>Object</code>&#xFF0C;&#x548C;<code>{}</code></h3>
<p>&#x4F60;&#x53EF;&#x80FD;&#x4F1A;&#x8BD5;&#x56FE;&#x4F7F;&#x7528;<code>Object</code>&#x6216;<code>{}</code>&#x6765;&#x8868;&#x793A;&#x4E00;&#x4E2A;&#x503C;&#x53EF;&#x4EE5;&#x5177;&#x6709;&#x4EFB;&#x610F;&#x5C5E;&#x6027;&#xFF0C;&#x56E0;&#x4E3A;<code>Object</code>&#x662F;&#x6700;&#x901A;&#x7528;&#x7684;&#x7C7B;&#x578B;&#x3002;
&#x7136;&#x800C;&#x5728;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x4E0B;<strong><code>any</code>&#x662F;&#x771F;&#x6B63;&#x60F3;&#x8981;&#x4F7F;&#x7528;&#x7684;&#x7C7B;&#x578B;</strong>&#xFF0C;&#x56E0;&#x4E3A;&#x5B83;&#x662F;&#x6700;<em>&#x7075;&#x6D3B;</em>&#x7684;&#x7C7B;&#x578B;&#x3002;</p>
<p>&#x6BD4;&#x5982;&#xFF0C;&#x6709;&#x4E00;&#x4E2A;<code>Object</code>&#x7C7B;&#x578B;&#x7684;&#x4E1C;&#x897F;&#xFF0C;&#x4F60;&#x5C06;&#x4E0D;&#x80FD;&#x591F;&#x5728;&#x5176;&#x4E0A;&#x8C03;&#x7528;<code>toLowerCase()</code>&#x3002;</p>
<p>&#x8D8A;&#x666E;&#x901A;&#x610F;&#x5473;&#x7740;&#x66F4;&#x5C11;&#x7684;&#x5229;&#x7528;&#x7C7B;&#x578B;&#xFF0C;&#x4F46;&#x662F;<code>any</code>&#x6BD4;&#x8F83;&#x7279;&#x6B8A;&#xFF0C;&#x5B83;&#x662F;&#x6700;&#x666E;&#x901A;&#x7684;&#x7C7B;&#x578B;&#x4F46;&#x662F;&#x5141;&#x8BB8;&#x4F60;&#x5728;&#x4E0A;&#x9762;&#x505A;&#x4EFB;&#x4F55;&#x4E8B;&#x60C5;&#x3002;
&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#x4F60;&#x53EF;&#x4EE5;&#x5728;&#x4E0A;&#x9762;&#x8C03;&#x7528;&#xFF0C;&#x6784;&#x9020;&#x5B83;&#xFF0C;&#x8BBF;&#x95EE;&#x5B83;&#x7684;&#x5C5E;&#x6027;&#x7B49;&#x7B49;&#x3002;
&#x8BB0;&#x4F4F;&#xFF0C;&#x5F53;&#x4F60;&#x4F7F;&#x7528;<code>any</code>&#x65F6;&#xFF0C;&#x4F60;&#x4F1A;&#x5931;&#x53BB;&#x5927;&#x591A;&#x6570;TypeScript&#x63D0;&#x4F9B;&#x7684;&#x9519;&#x8BEF;&#x68C0;&#x67E5;&#x548C;&#x7F16;&#x8BD1;&#x5668;&#x652F;&#x6301;&#x3002;</p>
<p>&#x5982;&#x679C;&#x4F60;&#x8FD8;&#x662F;&#x51B3;&#x5B9A;&#x4F7F;&#x7528;<code>Object</code>&#x548C;<code>{}</code>&#xFF0C;&#x4F60;&#x5E94;&#x8BE5;&#x9009;&#x62E9;<code>{}</code>&#x3002;
&#x867D;&#x8BF4;&#x5B83;&#x4EEC;&#x57FA;&#x672C;&#x4E00;&#x6837;&#xFF0C;&#x4F46;&#x662F;&#x4ECE;&#x6280;&#x672F;&#x89D2;&#x5EA6;&#x4E0A;&#x6765;&#x8BB2;<code>{}</code>&#x5728;&#x4E00;&#x4E9B;&#x6DF1;&#x5965;&#x7684;&#x60C5;&#x51B5;&#x91CC;&#x6BD4;<code>Object</code>&#x66F4;&#x666E;&#x901A;&#x3002;</p>
<h2 id="&#x542F;&#x7528;&#x4E25;&#x683C;&#x68C0;&#x67E5;"><a name="getting-stricter-checks"></a>&#x542F;&#x7528;&#x4E25;&#x683C;&#x68C0;&#x67E5;</h2>
<p>TypeScript&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x4E9B;&#x68C0;&#x67E5;&#x6765;&#x4FDD;&#x8BC1;&#x5B89;&#x5168;&#x4EE5;&#x53CA;&#x5E2E;&#x52A9;&#x5206;&#x6790;&#x4F60;&#x7684;&#x7A0B;&#x5E8F;&#x3002;
&#x5F53;&#x4F60;&#x5C06;&#x4EE3;&#x7801;&#x8F6C;&#x6362;&#x4E3A;&#x4E86;TypeScript&#x540E;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x542F;&#x7528;&#x8FD9;&#x4E9B;&#x68C0;&#x67E5;&#x6765;&#x5E2E;&#x52A9;&#x4F60;&#x83B7;&#x5F97;&#x9AD8;&#x5EA6;&#x5B89;&#x5168;&#x6027;&#x3002;</p>
<h3 id="&#x6CA1;&#x6709;&#x9690;&#x5F0F;&#x7684;any">&#x6CA1;&#x6709;&#x9690;&#x5F0F;&#x7684;<code>any</code></h3>
<p>&#x5728;&#x67D0;&#x4E9B;&#x60C5;&#x51B5;&#x4E0B;TypeScript&#x6CA1;&#x6CD5;&#x786E;&#x5B9A;&#x67D0;&#x4E9B;&#x503C;&#x7684;&#x7C7B;&#x578B;&#x3002;
&#x90A3;&#x4E48;TypeScript&#x4F1A;&#x4F7F;&#x7528;<code>any</code>&#x7C7B;&#x578B;&#x4EE3;&#x66FF;&#x3002;
&#x8FD9;&#x5BF9;&#x4EE3;&#x7801;&#x8F6C;&#x6362;&#x6765;&#x8BB2;&#x662F;&#x4E0D;&#x9519;&#xFF0C;&#x4F46;&#x662F;&#x4F7F;&#x7528;<code>any</code>&#x610F;&#x5473;&#x7740;&#x5931;&#x53BB;&#x4E86;&#x7C7B;&#x578B;&#x5B89;&#x5168;&#x4FDD;&#x969C;&#xFF0C;&#x5E76;&#x4E14;&#x4F60;&#x5F97;&#x4E0D;&#x5230;&#x5DE5;&#x5177;&#x7684;&#x652F;&#x6301;&#x3002;
&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>noImplicitAny</code>&#x9009;&#x9879;&#xFF0C;&#x8BA9;TypeScript&#x6807;&#x8BB0;&#x51FA;&#x53D1;&#x751F;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x7684;&#x5730;&#x65B9;&#xFF0C;&#x5E76;&#x7ED9;&#x51FA;&#x4E00;&#x4E2A;&#x9519;&#x8BEF;&#x3002;</p>
<h3 id="&#x4E25;&#x683C;&#x7684;null&#x4E0E;undefined&#x68C0;&#x67E5;">&#x4E25;&#x683C;&#x7684;<code>null</code>&#x4E0E;<code>undefined</code>&#x68C0;&#x67E5;</h3>
<p>&#x9ED8;&#x8BA4;&#x5730;&#xFF0C;TypeScript&#x628A;<code>null</code>&#x548C;<code>undefined</code>&#x5F53;&#x505A;&#x5C5E;&#x4E8E;&#x4EFB;&#x4F55;&#x7C7B;&#x578B;&#x3002;
&#x8FD9;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;&#x58F0;&#x660E;&#x4E3A;<code>number</code>&#x7C7B;&#x578B;&#x7684;&#x503C;&#x53EF;&#x4EE5;&#x4E3A;<code>null</code>&#x548C;<code>undefined</code>&#x3002;
&#x56E0;&#x4E3A;&#x5728;JavaScript&#x548C;TypeScript&#x91CC;&#xFF0C;<code>null</code>&#x548C;<code>undefined</code>&#x7ECF;&#x5E38;&#x4F1A;&#x5BFC;&#x81F4;BUG&#x7684;&#x4EA7;&#x751F;&#xFF0C;&#x6240;&#x4EE5;TypeScript&#x5305;&#x542B;&#x4E86;<code>strictNullChecks</code>&#x9009;&#x9879;&#x6765;&#x5E2E;&#x52A9;&#x6211;&#x4EEC;&#x51CF;&#x5C11;&#x5BF9;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x7684;&#x62C5;&#x5FE7;&#x3002;</p>
<p>&#x5F53;&#x542F;&#x7528;&#x4E86;<code>strictNullChecks</code>&#xFF0C;<code>null</code>&#x548C;<code>undefined</code>&#x83B7;&#x5F97;&#x4E86;&#x5B83;&#x4EEC;&#x81EA;&#x5DF1;&#x5404;&#x81EA;&#x7684;&#x7C7B;&#x578B;<code>null</code>&#x548C;<code>undefined</code>&#x3002;
&#x5F53;&#x4EFB;&#x4F55;&#x503C;<em>&#x53EF;&#x80FD;</em>&#x4E3A;<code>null</code>&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x8054;&#x5408;&#x7C7B;&#x578B;&#x3002;
&#x6BD4;&#x5982;&#xFF0C;&#x67D0;&#x503C;&#x53EF;&#x80FD;&#x4E3A;<code>number</code>&#x6216;<code>null</code>&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x58F0;&#x660E;&#x5B83;&#x7684;&#x7C7B;&#x578B;&#x4E3A;<code>number | null</code>&#x3002;</p>
<p>&#x5047;&#x8BBE;&#x6709;&#x4E00;&#x4E2A;&#x503C;TypeScript&#x8BA4;&#x4E3A;&#x53EF;&#x4EE5;&#x4E3A;<code>null</code>&#x6216;<code>undefined</code>&#xFF0C;&#x4F46;&#x662F;&#x4F60;&#x66F4;&#x6E05;&#x695A;&#x5B83;&#x7684;&#x7C7B;&#x578B;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>!</code>&#x540E;&#x7F00;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">declare</span> <span class="hljs-keyword">var</span> foo: <span class="hljs-built_in">string</span>[] | <span class="hljs-literal">null</span>;

foo.length;  <span class="hljs-comment">// error - &apos;foo&apos; is possibly &apos;null&apos;</span>

foo!.length; <span class="hljs-comment">// okay - &apos;foo!&apos; just has type &apos;string[]&apos;</span>
</code></pre>
<p>&#x8981;&#x5F53;&#x5FC3;&#xFF0C;&#x5F53;&#x4F60;&#x4F7F;&#x7528;<code>strictNullChecks</code>&#xFF0C;&#x4F60;&#x7684;&#x4F9D;&#x8D56;&#x4E5F;&#x9700;&#x8981;&#x76F8;&#x5E94;&#x5730;&#x542F;&#x7528;<code>strictNullChecks</code>&#x3002;</p>
<h3 id="this&#x6CA1;&#x6709;&#x9690;&#x5F0F;&#x7684;any"><code>this</code>&#x6CA1;&#x6709;&#x9690;&#x5F0F;&#x7684;<code>any</code></h3>
<p>&#x5F53;&#x4F60;&#x5728;&#x7C7B;&#x7684;&#x5916;&#x90E8;&#x4F7F;&#x7528;<code>this</code>&#x5173;&#x952E;&#x5B57;&#x65F6;&#xFF0C;&#x5B83;&#x4F1A;&#x9ED8;&#x8BA4;&#x83B7;&#x5F97;<code>any</code>&#x7C7B;&#x578B;&#x3002;
&#x6BD4;&#x5982;&#xFF0C;&#x5047;&#x8BBE;&#x6709;&#x4E00;&#x4E2A;<code>Point</code>&#x7C7B;&#xFF0C;&#x5E76;&#x4E14;&#x6211;&#x4EEC;&#x8981;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x505A;&#x4E3A;&#x5B83;&#x7684;&#x65B9;&#x6CD5;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">class</span> Point {
    <span class="hljs-keyword">constructor</span>(public x, public y) {}
    getDistance(p: Point) {
        <span class="hljs-keyword">let</span> dx = p.x - <span class="hljs-keyword">this</span>.x;
        <span class="hljs-keyword">let</span> dy = p.y - <span class="hljs-keyword">this</span>.y;
        <span class="hljs-keyword">return</span> <span class="hljs-built_in">Math</span>.sqrt(dx ** <span class="hljs-number">2</span> + dy ** <span class="hljs-number">2</span>);
    }
}
<span class="hljs-comment">// ...</span>

<span class="hljs-comment">// Reopen the interface.</span>
<span class="hljs-keyword">interface</span> Point {
    distanceFromOrigin(point: Point): <span class="hljs-built_in">number</span>;
}
Point.prototype.distanceFromOrigin = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">point: Point</span>) </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.getDistance({ x: <span class="hljs-number">0</span>, y: <span class="hljs-number">0</span>});
}
</code></pre>
<p>&#x8FD9;&#x5C31;&#x4EA7;&#x751F;&#x4E86;&#x6211;&#x4EEC;&#x4E0A;&#x9762;&#x63D0;&#x5230;&#x7684;&#x9519;&#x8BEF; - &#x5982;&#x679C;&#x6211;&#x4EEC;&#x9519;&#x8BEF;&#x5730;&#x62FC;&#x5199;&#x4E86;<code>getDistance</code>&#x5E76;&#x4E0D;&#x4F1A;&#x5F97;&#x5230;&#x4E00;&#x4E2A;&#x9519;&#x8BEF;&#x3002;
&#x6B63;&#x56E0;&#x6B64;&#xFF0C;TypeScript&#x6709;<code>noImplicitThis</code>&#x9009;&#x9879;&#x3002;
&#x5F53;&#x8BBE;&#x7F6E;&#x4E86;&#x5B83;&#xFF0C;TypeScript&#x4F1A;&#x4EA7;&#x751F;&#x4E00;&#x4E2A;&#x9519;&#x8BEF;&#x5F53;&#x6CA1;&#x6709;&#x660E;&#x786E;&#x6307;&#x5B9A;&#x7C7B;&#x578B;&#xFF08;&#x6216;&#x901A;&#x8FC7;&#x7C7B;&#x578B;&#x63A8;&#x65AD;&#xFF09;&#x7684;<code>this</code>&#x88AB;&#x4F7F;&#x7528;&#x65F6;&#x3002;
&#x89E3;&#x51B3;&#x7684;&#x65B9;&#x6CD5;&#x662F;&#x5728;&#x63A5;&#x53E3;&#x6216;&#x51FD;&#x6570;&#x4E0A;&#x4F7F;&#x7528;&#x6307;&#x5B9A;&#x4E86;&#x7C7B;&#x578B;&#x7684;<code>this</code>&#x53C2;&#x6570;&#xFF1A;</p>
<pre><code class="lang-ts">Point.prototype.distanceFromOrigin = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"><span class="hljs-keyword">this</span>: Point, point: Point</span>) </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.getDistance({ x: <span class="hljs-number">0</span>, y: <span class="hljs-number">0</span>});
}
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="Angular 2.html" class="navigation navigation-prev " aria-label="Previous page: Angular 2">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../" class="navigation navigation-next " aria-label="Next page: 手册">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"从JavaScript迁移到TypeScript","level":"1.2.9","depth":2,"next":{"title":"手册","level":"1.3","depth":1,"path":"doc/handbook/README.md","ref":"./doc/handbook/README.md","articles":[{"title":"基础类型","level":"1.3.1","depth":2,"path":"doc/handbook/Basic Types.md","ref":"./doc/handbook/Basic Types.md","articles":[]},{"title":"变量声明","level":"1.3.2","depth":2,"path":"doc/handbook/Variable Declarations.md","ref":"./doc/handbook/Variable Declarations.md","articles":[]},{"title":"接口","level":"1.3.3","depth":2,"path":"doc/handbook/Interfaces.md","ref":"./doc/handbook/Interfaces.md","articles":[]},{"title":"类","level":"1.3.4","depth":2,"path":"doc/handbook/Classes.md","ref":"./doc/handbook/Classes.md","articles":[]},{"title":"函数","level":"1.3.5","depth":2,"path":"doc/handbook/Functions.md","ref":"./doc/handbook/Functions.md","articles":[]},{"title":"泛型","level":"1.3.6","depth":2,"path":"doc/handbook/Generics.md","ref":"./doc/handbook/Generics.md","articles":[]},{"title":"枚举","level":"1.3.7","depth":2,"path":"doc/handbook/Enums.md","ref":"./doc/handbook/Enums.md","articles":[]},{"title":"类型推论","level":"1.3.8","depth":2,"path":"doc/handbook/Type Inference.md","ref":"./doc/handbook/Type Inference.md","articles":[]},{"title":"类型兼容性","level":"1.3.9","depth":2,"path":"doc/handbook/Type Compatibility.md","ref":"./doc/handbook/Type Compatibility.md","articles":[]},{"title":"高级类型","level":"1.3.10","depth":2,"path":"doc/handbook/Advanced Types.md","ref":"./doc/handbook/Advanced Types.md","articles":[]},{"title":"实用工具类型","level":"1.3.11","depth":2,"path":"doc/handbook/Utilit Types.md","ref":"./doc/handbook/Utilit Types.md","articles":[]},{"title":"Symbols","level":"1.3.12","depth":2,"path":"doc/handbook/Symbols.md","ref":"./doc/handbook/Symbols.md","articles":[]},{"title":"Iterators 和 Generators","level":"1.3.13","depth":2,"path":"doc/handbook/Iterators and Generators.md","ref":"./doc/handbook/Iterators and Generators.md","articles":[]},{"title":"模块","level":"1.3.14","depth":2,"path":"doc/handbook/Modules.md","ref":"./doc/handbook/Modules.md","articles":[]},{"title":"命名空间","level":"1.3.15","depth":2,"path":"doc/handbook/Namespaces.md","ref":"./doc/handbook/Namespaces.md","articles":[]},{"title":"命名空间和模块","level":"1.3.16","depth":2,"path":"doc/handbook/Namespaces and Modules.md","ref":"./doc/handbook/Namespaces and Modules.md","articles":[]},{"title":"模块解析","level":"1.3.17","depth":2,"path":"doc/handbook/Module Resolution.md","ref":"./doc/handbook/Module Resolution.md","articles":[]},{"title":"声明合并","level":"1.3.18","depth":2,"path":"doc/handbook/Declaration Merging.md","ref":"./doc/handbook/Declaration Merging.md","articles":[]},{"title":"书写.d.ts文件","level":"1.3.19","depth":2,"path":"doc/handbook/Writing Definition Files.md","ref":"./doc/handbook/Writing Definition Files.md","articles":[]},{"title":"JSX","level":"1.3.20","depth":2,"path":"doc/handbook/JSX.md","ref":"./doc/handbook/JSX.md","articles":[]},{"title":"Decorators","level":"1.3.21","depth":2,"path":"doc/handbook/Decorators.md","ref":"./doc/handbook/Decorators.md","articles":[]},{"title":"混入","level":"1.3.22","depth":2,"path":"doc/handbook/Mixins.md","ref":"./doc/handbook/Mixins.md","articles":[]},{"title":"三斜线指令","level":"1.3.23","depth":2,"path":"doc/handbook/Triple-Slash Directives.md","ref":"./doc/handbook/Triple-Slash Directives.md","articles":[]},{"title":"JavaScript文件里的类型检查","level":"1.3.24","depth":2,"path":"doc/handbook/Type Checking JavaScript Files.md","ref":"./doc/handbook/Type Checking JavaScript Files.md","articles":[]}]},"previous":{"title":"Angular 2","level":"1.2.8","depth":2,"path":"doc/handbook/tutorials/Angular 2.md","ref":"./doc/handbook/tutorials/Angular 2.md","articles":[]},"dir":"ltr"},"config":{"plugins":[],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"Patrick Zhong","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"preface.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"TypeScript Handbook","language":"zh-hans","gitbook":">3.0.0","description":"Translation of official TypeScript Handbook"},"file":{"path":"doc/handbook/tutorials/Migrating from JavaScript.md","mtime":"2018-10-13T09:48:54.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-10-13T09:57:42.904Z"},"basePath":"../../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../../gitbook/gitbook.js"></script>
    <script src="../../../gitbook/theme.js"></script>
    
        
        <script src="../../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

